package com.wind.ipagent.utils;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;

import java.io.File;
import java.io.IOException;

public class ObjectToJsonFileUtil {

    public static void writeValue(String filePath,String fileName,Object object){
        ObjectMapper mapper = new ObjectMapper().disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);;
        File file = new File(filePath);
        try {
            if(!file.exists()){
                file.mkdirs();
            }
            file = new File(filePath+File.separator+fileName);
            // 序列化Java对象信息JSON文件。
            mapper.writeValue(file, object);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static Object readObjectValue(String filePath,String fileName,Class classType){
        ObjectMapper mapper = new ObjectMapper().disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);;
        File file = new File(filePath);
        Object object =null;
        try {
            if(!file.exists()){
                file.mkdirs();
            }
            file = new File(filePath+File.separator+fileName);
            // 将JSON文件反序列化为Java对象。
            object = mapper.readValue(file, classType);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return  object;
    }


}
